﻿/*  
 * Helix Engine
 * http://helixengine.codeplex.com/
 * 
 * Copyright (c) 2010 - 2011 Joe Hegarty
 *  
 * This source code is subject to the terms of the Microsoft Public License (Ms-PL). 
 *  
 * Redistribution and use in source and binary forms, with or without modification, 
 * is permitted provided that redistributions of the source code retain the above 
 * copyright notices and this file header. 
 * 
 * For details please see http://www.opensource.org/licenses/ms-pl.html 
 *  
 * All other rights reserved. 
 *
 */  


using System;
using System.Collections.Generic;

using HelixEngine.Maths;

using HelixGame.World.Entities;

namespace HelixGame.World
{
    /// <remarks>
    /// Manages the game world
    /// </remarks>
    public class WorldManager
    {
        List<Entity> _entities = new List<Entity>();

        bool _entityListLocked = false;
        List<Entity> _entitiesToAdd = new List<Entity>();
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="roomId"></param>
        public void LoadRoom(int roomId)
        {
            Entity floor = AddEntity("FloorMap");
            floor.AddEC<HelixGame.World.Entities.Components.FloorComponent>();
            

            Entity mouse = AddEntity("Mouse");
            HelixGame.World.Entities.Components.HighlightComponent highlightComponent = new HelixGame.World.Entities.Components.HighlightComponent(floor);
            HelixGame.World.Entities.Components.ObjectPlacerComponent objectComponent = new HelixGame.World.Entities.Components.ObjectPlacerComponent(floor);
            mouse.AddEC(highlightComponent);
            mouse.AddEC(objectComponent);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="time"></param>
        public void Update(EngineTime time)
        {
            _entityListLocked = true;
            foreach (var entity in _entities)
            {
                entity.Update(time);
            }
            _entityListLocked = false;

            foreach (var entity in _entitiesToAdd)
            {
                _entities.Add(entity);
            }

            _entitiesToAdd.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="archetype"></param>
        /// <returns></returns>
        public Entity AddEntity(string archetype)
        {
            Entity newEntity = new Entity(archetype);
            if (_entityListLocked)
            {
                _entitiesToAdd.Add(newEntity);
            }
            else
            {
                _entities.Add(newEntity);
            }
            
            
            return newEntity;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Unload()
        {
            foreach (var entity in _entities)
            {
                entity.Cleanup();
            }

            _entities.Clear();
        }
    }
}
